package com.factors.HuangHePing.Access_to_ResourcesAvg;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class AverageMapper extends Mapper<LongWritable, Text, Text, IntWritable> {//LongWritable（表示字节偏移量，输入值类型为 Text），输出键类型为 Text，输出值类型为 IntWritable。

    private Text sourceType = new Text();
    private IntWritable examScore = new IntWritable();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String[] fields = value.toString().split(",");

        if (fields.length > 15) {
            sourceType.set(fields[3]); // sourceType是第4个字段（索引从0开始，故为12）
            examScore.set(Integer.parseInt(fields[19])); // exam_Score是第20个字段（索引为19）
            context.write(sourceType, examScore);
        }
    }
}